package com.hopu.dxhouse.service;

import com.hopu.dxhouse.entity.MyEmp;
import com.hopu.dxhouse.mapper.MyEmpDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Package: com.hopu.dxhouse.service
 * @ClassName: MyEmpServiceImple
 * @Author: Administrator
 * @CreateTime: 2020/10/19 16:54
 * @Description:
 */
public class MyEmpServiceImple implements MyEmpService {
    //初始化持久层（Dao层
    MyEmpDao myEmpDao;

    public MyEmpServiceImple(){ //使用构造器将初始化方法在调用Service层的时候使用
        init();
    }

    public void init(){
        try {
            /*注册Mybatis的配置文件*/
            InputStream input = null;

            input = Resources.getResourceAsStream("sqlMapConfig.xml");

            /*创建SqlSessionFactoryBuilder*/
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

            /*通过创建SqlSessionFactoryBuilder创建SqlSessionFactory*/
            SqlSessionFactory factory = builder.build(input);

            /*通过Session工厂创建SqlSession对象*/
            SqlSession sqlSession = factory.openSession();

            /*通过SqlSession创建Dao的代理对象：代理的实现类去执行Mapper.xml文件中对应的SQL语句*/
            myEmpDao = sqlSession.getMapper(MyEmpDao.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public MyEmp checkEname(String ename) {
        return myEmpDao.checkEname(ename);
    }

    @Override
    public MyEmp checkEnameAndEpsw(String ename, String epsw) {
        return myEmpDao.checkEnameAndEpsw(ename,epsw);
    }
}
